package jp.mixi.android.sdk.hackathon.util;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.util.Log;

public class PackageUtil {

	public static String getSignature(Context context){

        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(),
                    PackageManager.GET_SIGNATURES);
            Signature[] sigs = pi.signatures;
            for (Signature sig : sigs) {
                byte[] digest = MessageDigest.getInstance("SHA1").digest(sig.toByteArray());
                // sig.toByteArray();
                StringBuilder b = new StringBuilder();
                for (byte by : digest) {
                    String h = Integer.toHexString(by & 0xff);
                    if (h.length() == 1) {
                        b.append('0');
                    }
                    b.append(h);
                }
                Log.v("signature", b.toString());
                return b.toString();
            }
        } catch (NameNotFoundException e) {
        } catch (NoSuchAlgorithmException e) {
        }
        return null;

	}
}
